package com.策略模式2;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2022/10/27 13:36
 * @description:
 * @version:
 */
@Component
public class FactoryDemo implements ApplicationContextAware {

    private static Map<EnumDemo,InterfaceDemo> demoMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        demoMap = new HashMap<>();
        Map<String, InterfaceDemo> beansOfType = applicationContext.getBeansOfType(InterfaceDemo.class);
        for (Map.Entry<String, InterfaceDemo> entry : beansOfType.entrySet()) {
            EnumDemo curEnum = entry.getValue().getEnum();
            if (curEnum == null){
                continue;
            }
            demoMap.put(curEnum,entry.getValue());
        }
    }

    public static < T extends InterfaceDemo> T getDemoImpl(EnumDemo curEnum){
        return (T)demoMap.get(curEnum);
    }
}

